package 模拟;

public class 外观数列4 {
    //https://leetcode.cn/problems/count-and-say/description/
    public String countAndSay(int n) {
        String s = "1";
        for (int i = 1; i < n; i++) {
            StringBuilder ss = new StringBuilder();
            for(int left = 0, right = 0;right < s.length();) {
                while(right < s.length() && s.charAt(left) == s.charAt(right)) {
                    right++;
                }
                ss.append(right - left);
                ss.append(s.charAt(left));
                left = right;
            }
            s = ss.toString();
        }
        return s;
    }
}
